package tech.aistar.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.dto.Parper;
import tech.aistar.entity.Completion;
import tech.aistar.entity.Manchoice;
import tech.aistar.entity.Programming;
import tech.aistar.entity.Sinchoice;
import tech.aistar.service.*;
import tech.aistar.util.CompilerUtil;

import java.util.List;

/**
 * 本类包含：
 * 1.
 */

@Controller
@RequestMapping("/parper")
public class ParperAction {

    //试卷库
    @Autowired
    private IQuestionpaperService questionpaperService;
    //单选
    @Autowired
    public ISinchoiceService sinchoiceService;
    //多选
    @Autowired
    private IManchoiceService manchoiceService;
    //填空
    @Autowired
    private ICompletionService completionService;
    //编程
    @Autowired
    public IProgrammingService programmingService;

    //在试卷详情页展示题目数量
    @RequestMapping("/start")
    public String  count(Model model,Long parperId){
        parperId = 1L;
        Long sin = questionpaperService.countType(parperId, 1L);
        Long man = questionpaperService.countType(parperId, 2L);
        Long cmo = questionpaperService.countType(parperId, 3L);
        Long pro = questionpaperService.countType(parperId, 4L);
        Long count[] = {sin, man, cmo, pro, parperId};
        model.addAttribute("count",count);
        return  "start";
    }

    //
    @RequestMapping("/exaInfo")
    public String exaInfo(Model model,Long parperId,String value){
        return null;
    }
    @RequestMapping("/comp")
    public @ResponseBody JSONObject run(@RequestParam String code,@RequestParam String lang){
//        System.out.println(code);
//        System.out.println(lang);
//
        CompilerUtil compilerUtil = new CompilerUtil();
//        System.out.println(compilerUtil.complier(code, lang));

        return  JSONObject.parseObject(compilerUtil.complier(code,lang));
    }




    //各种题型之间的跳转
//    @RequestMapping("/Questionpaper")
//    public String  Questionpaper(Model model , String url,Long parperId){
//        model.addAttribute("parperId",1);
//        return  "redirect:/parper/"+url+".action";
//    }


    //定义试卷类来存放要传到前端的试卷
    Parper parper = new Parper();

    //单选题
    @RequestMapping("/choose")
    public String Sinchoice(Model model, Long parperId){
        List<Sinchoice> sinchoiceList = sinchoiceService.findAll();
        Sinchoice byId = sinchoiceService.findById(1L);

        List<Sinchoice> byPaperId = sinchoiceService.findByPaperId(parperId);//通过试卷号查找
        parper.setParperId(parperId);//试卷号
        parper.setSinchoiceList(byPaperId);//将单选题放入试卷对象
        model.addAttribute("parper", JSON.toJSON(parper));
        return "choose";
    }

    //多选题
    @RequestMapping("/manchoose")
    public String Manchoice(Model model , Long parperId){
        List<Manchoice> manchoiceList = manchoiceService.findAll();
        Manchoice byId = manchoiceService.findById(1L);
        List<Manchoice> byPaperId = manchoiceService.findByPaperId(parperId);
        parper.setManchoiceList(byPaperId);//将多选题放入试卷对象
        model.addAttribute("parper", JSON.toJSON(parper) );
        return "manchoose";
    }

    //填空题
    @RequestMapping("/fillTheBlank")
    public String Completion(Model model, Long parperId){
        Completion completionList = completionService.findById(parperId);

        List<Completion> byPaperId = completionService.findByPaperId(parperId);
        parper.setCompletionList(byPaperId);//将填空题放入对象
        model.addAttribute("parper", JSON.toJSON(parper) );
        return "fillTheBlank";
    }

    //编程题
    @RequestMapping("/programe")
    public String Programming(Model model , Long parperId){
        Programming programmingList = programmingService.findById(parperId);
        List<Programming> all = programmingService.findAll();
        List<Programming> byPaperId = programmingService.findByPaperId(1L);
        parper.setProgrammingList(byPaperId);//将编程题放入对象
        model.addAttribute("parper",JSON.toJSON(parper));
        return "programe";
    }

}
